xquery version "1.0";

module namespace layout="http://www.example.com/layout";

import module namespace addf="http://www.example.com/addf" at "xmldb:exist:///db/projdb/modules/addf.xqm";
import module namespace request="http://exist-db.org/xquery/request";

declare function layout:topMenu() as element() {

let $filename := addf:substring-after-last(xs:string(request:get-effective-uri()), '/')
return <div id="topMenu">
        <ul class="menu">
            <li><a href="index.xql">
            {if ($filename eq 'index.xql') then attribute class {'firstItem active'} else attribute class {'firstItem'}}
            Overzicht</a></li>
            <li><a href="activity.xql">
            {if ($filename eq 'activity.xql') then attribute class {'active'} else ()}
            Activiteitsgraad</a></li>
            <li><a href="ranking.xql">
            {if ($filename eq 'ranking.xql') then attribute class {'active'} else ()}Ranking</a></li>
            <li><a href="partyIndex.xql">
            {if ($filename eq 'partyIndex.xql') then attribute class {'active'} else ()}Partijen</a></li>
            <li><a href="sexIndex.xql">
            {if ($filename eq 'sexIndex.xql') then attribute class {'active'} else ()}Geslacht</a></li>
            <li><a href="mayorIndex.xql">
            {if ($filename eq 'mayorIndex.xql') then attribute class {'active'} else ()}Burgemeesters</a></li>
            <li><a href="subjects.xql">
            {if ($filename eq 'subjects.xql') then attribute class {'active'} else ()}Onderwerpen</a></li>
        </ul>
        <br class="clearer" />
    </div>
};

declare function layout:titleBox($title as xs:string) as element() {

    <div class="box" id="titleBox">
        <div class="margin">
            <h1>{$title}</h1>
        </div>
    </div>
};

declare function layout:anonymousContentBox($id as xs:string, $title as xs:string, $content as element()) as element() {
    layout:_contentBox($id, $title, $content, false())
};

declare function layout:contentBox($id as xs:string, $title as xs:string, $content as element()) as element() {
    layout:_contentBox($id, $title, $content, true())
};

declare function layout:twoColumns($columnDescription1 as element(), $columnDescription2 as element()) as element() {
    <div class="columnspace twoColumns">
        {layout:_addClass($columnDescription1, "firstColumn"), 
         layout:_addClass($columnDescription2, "secondColumn")}
        <br class="clearer" />
    </div>
};

declare function layout:twoColumnsDark($columnDescription1 as element(), $columnDescription2 as element()) as element() {
    <div class="columnspace twoColumns darkBackground">
        {layout:_addClass($columnDescription1, "firstColumn"), 
         layout:_addClass($columnDescription2, "secondColumn")}
        <br class="clearer" />
    </div>
};

declare function layout:columnDescription($title as xs:string, $content as element()) as element() {
    <div class="column activityChart firstColumn">
        <h3 class="columnTitle">{$title}</h3>
        {$content}
    </div>
};

declare function layout:_addClass($element as element(), $class as xs:string) as element(){
    element {name($element)} {
        for $attr in $element/@*
            return attribute {name($attr)} {data($attr)},
        $element/*
    }

};

declare function layout:_contentBox($id as xs:string, $title as xs:string, $content as element(), $showTitle as xs:boolean) as element() {
    <div class="box" id="{$id}">
        <div class="margin">
            {if($showTitle eq fn:true()) then <h2 class="boxTitle">{$title}</h2> else <h2 class="boxTitle hiddenElement">{$title}</h2>}
            {$content}
        </div>
    </div>
};
